<?php
namespace App\Models\Mysql;

use App\Models\Admin\InterfaceConfiguration;
use App\Recharge\Enums\RechargeStatusEnum;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasOne;
use function Swoole\Coroutine\Http\get;

/**
* @comment 订单循环表
*
* @property int $id 主键
* @property string $order_no 主订单号
* @property string $trade_no 下单订单号
* @property string $other_order 第三方订单号
* @property int $pid 主商品id
* @property int $gid 商品id
* @property string $good_name 商品名称
* @property string $good_code 商品编码
* @property int $recharge_num 充值数量
* @property float $unitPrice 进价
* @property int $state 状态:0=下单中,1=成功,2=失败
* @property int $supplier_status 商品归属
* @property string $success_time 成功时间
* @property string $error_time 失败时间
* @property string $error_msg 失败原因
* @property int $peak_failure 是否是峰值失败:1=是,2=不是
* @property string $create_time 创建时间
* @property string $update_time 修改时间
*
* @property InterfaceConfiguration $belong
*/
class CycleOrder extends BaseModel
{
    public const CREATED_AT = 'create_time';

    public const UPDATED_AT = 'update_time';

    /**
     * @var bool
     */
    public $timestamps = true;

    /**
     * @var string
     */
    protected $table = 'cycle_order';

    /**
     * @var string[]
    */
    protected $fillable = ['id', 'order_no', 'trade_no', 'other_order', 'pid', 'gid', 'good_name', 'good_code', 'recharge_num', 'unitPrice', 'state', 'supplier_status', 'success_time', 'error_time', 'error_msg', 'peak_failure', 'create_time', 'update_time'];

    /**
     * @return Attribute
     */
    protected function stateStatus(): Attribute
    {
        return Attribute::make(
            get: fn(mixed $value, array $attributes) => RechargeStatusEnum::tryFrom($attributes['state'] ?? 0)?->statusText()
        );
    }

    /**
     * @return HasOne
     */
    public function belong(): HasOne
    {
        return $this->hasOne(InterfaceConfiguration::class,  'id', 'supplier_status');
    }
}
